package com.chen.q4.router.impl;

import com.chen.q4.router.HttpEndpointRouter;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 循环路由
 * @author Charles
 */
public class CyclicHttpEndpointRouter implements HttpEndpointRouter {
    AtomicInteger currentEndpoint = new AtomicInteger(0);

    @Override
    public String route(List<String> endpointList) {
        int size = endpointList.size();
        int index = currentEndpoint.getAndIncrement();
        if (index >= size){
            index = 0;
            currentEndpoint.set(0);
        }
        System.out.println(index);
        return endpointList.get(index);
    }
}
